package edu.berkeley.cs160.GroupC;


import java.util.Comparator;

import edu.berkeley.cs160.GroupC.adapter.PlaylistAdapter;
import edu.berkeley.cs160.GroupC.songs.Song;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

public class AddSongActivity extends ListActivity {
	private WorkoutMusicApplication app;
	private PlaylistAdapter adapter;
	private Button sortByBpmButton;
	private Button sortByArtistButton;
	private Button sortByTitleButton;
	
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.playlist);
        
        
        
        app = (WorkoutMusicApplication)getApplication();
        
        adapter = new PlaylistAdapter(app.getCurrentPlaylist(), this);
        
        setListAdapter(adapter);
        
        setUpViews();
        
	}

	protected void onResume() {
		super.onResume();
		setUpViews();
		adapter.forceReload();
	}

	private void setUpViews() {
		sortByBpmButton = (Button)findViewById(R.id.sortBpmButton);
		sortByArtistButton = (Button)findViewById(R.id.sortArtistButton);
		sortByTitleButton = (Button)findViewById(R.id.sortTitleButton);
		
		
		
		sortByBpmButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				BpmComparator c = new BpmComparator();
				adapter.sortByComparator(c);
				adapter.forceReload();
			}
		});
		sortByArtistButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				ArtistComparator c = new ArtistComparator();
				adapter.sortByComparator(c);
				adapter.forceReload();
			}
		});
		sortByTitleButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				TitleComparator c = new TitleComparator();
				adapter.sortByComparator(c);
				adapter.forceReload();
			}
		});
	}
	
	protected void onListItemClick(ListView l, View v, int position, long id){
		super.onListItemClick(l, v, position, id);
		adapter.toggleAddedAtPosition(position);
		Song song = (Song) adapter.getItem(position);
		
		if (song.isInPlaylist() && !song.isRated()){
			app.setNewSongAdded(song);
			Intent intent = new Intent(AddSongActivity.this, Rating.class);
			startActivity(intent);
		}
	}
	
	
	class BpmComparator implements Comparator<Song>{
		public int compare(Song song1, Song song2){
			int bpm1 = ((Song)song1).getBpm();
			int bpm2 = ((Song)song2).getBpm();
			
			if(bpm1 > bpm2)
				return 1;
			else if (bpm1 < bpm2)
				return -1;
			else
				return 0;
		}
	}

	class ArtistComparator implements Comparator<Song>{
		public int compare(Song song1, Song song2){
			String s1 = ((Song)song1).getArtist();
			String s2 = ((Song)song2).getArtist();
			int result = s1.compareTo(s2);
			if(result > 0)
				return 1;
			else if (result < 0)
				return -1;
			else
				return 0;
		}
	}
	
	class TitleComparator implements Comparator<Song>{
		public int compare(Song song1, Song song2){
			String s1 = ((Song)song1).getTitle();
			String s2 = ((Song)song2).getTitle();
			int result = s1.compareTo(s2);
			if(result > 0)
				return 1;
			else if (result < 0)
				return -1;
			else
				return 0;
		}
	}
	

	
}
